﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;

namespace Document_Management
{
    public partial class frm量具台帳 : Form
    {
        static string connectionString = ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        //int width, height;
        string flag;
        object img;
        string str = null;
        string tb1, tb2, tb3, tb4, tb5, tb6, tb7, tb8, tb9, tb10, tb11, tb12, tb13, tb14, tb15, tb16, tb17, tb18, tb19,tb20, dt1;
        bool b量具Search, b量具Insert, b量具Edit, b量具Del;
        public frm量具台帳(bool b量具Search, bool b量具Insert, bool b量具Edit, bool b量具Del)
        //public frm量具台帳()
        {
            InitializeComponent();
            this.b量具Search = b量具Search;
            this.b量具Insert = b量具Insert;
            this.b量具Edit = b量具Edit;
            this.b量具Del = b量具Del;
        }

        //Code 量具台帳
        private void frm量具台帳_Load(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                btn_add.Enabled = false;
                btnAddnews.Enabled = false;
                btnAddnews2.Enabled = false;
                btnAddnews3.Enabled = false;
                btnAddnews4.Enabled = false;
                btnAddnews5.Enabled = false;
                //
                btn_領用.Enabled = false;
                btn_借用.Enabled = false;
                btn_送檢.Enabled = false;
                btn_修理.Enabled = false;
                btn_報廢.Enabled = false;
                btn_校正.Enabled = false;
            }
            if (!b量具Search)
            {
                btn_search.Enabled = false;
            }
            if (!b量具Edit)
            {
                btn_edit.Enabled = false;
                btnEdit.Enabled = false;
                btnEdit2.Enabled = false;
                btnEdit3.Enabled = false;
                btnEdit4.Enabled = false;
                btnEdit5.Enabled = false;
            }
            if (!b量具Del)
            {
                btn_del.Enabled = false;
                btnDelete.Enabled = false;
                btnDelete2.Enabled = false;
                btnDelete3.Enabled = false;
                btnDelete4.Enabled = false;
                btnDelete5.Enabled = false;
            }
            SqlConnection con = new SqlConnection(connectionString);
            AddCommobox();
            Load量具訊息();
            Load校正類別訊息();
            Load請購人員訊息();
            Load存放地點訊息();
            Load廠牌訊息();
            Load量具類別訊息();
            //dataGV量具台帳.Columns[0].Width = 70;
        }

        public void AddCommobox()
        {
            cmb量具狀態.Text = "所有";
            cmb量具狀態.Items.Add("所有");
            cmb量具狀態.Items.Add("領用");
            cmb量具狀態.Items.Add("借用");
            cmb量具狀態.Items.Add("送檢");
            cmb量具狀態.Items.Add("修理");
            cmb量具狀態.Items.Add("報廢");
            //cmb量具狀態.Items.Add("維修");
            cmb量具狀態.Items.Add("校正");
        }

        public void ResetTextMeasureTool()
        {
            txt数量.ResetText();
            txt存放地點.ResetText();
            txt校正類別.ResetText();
            txt量具名稱.ResetText();
            txt量具類別.ResetText();
            //datefrom.ResetText();
            //todate.ResetText();
            //cmb量具狀態.ResetText();
            cmb量具狀態.Text = "所有";
        }

        private void tab量具台帳_SelectedIndexChanged(object sender, EventArgs e)
        {
            ResetTextMeasureTool();

            PublicClass.setRowNumber(dataGV量具台帳);
            //dataGV量具台帳.Columns[0].Width = 70;
            PublicClass.setRowNumber(dataGV校正類別訊息);
            //dataGV校正類別訊息.Columns[0].Width = 70;
            PublicClass.setRowNumber(dataGV請購人員);
            //dataGV請購人員.Columns[0].Width = 70;
            PublicClass.setRowNumber(dataGV存放地點);
            //dataGV存放地點.Columns[0].Width = 70;
            PublicClass.setRowNumber(dataGV廠牌);
            //dataGV廠牌.Columns[0].Width = 70;
            PublicClass.setRowNumber(dataGV量具類別);
            //dataGV量具類別.Columns[0].Width = 70;
            
            //ResetText校正類別訊息
            txt校正類別代號.ResetText();
            txt校正類別名稱.ResetText();
            //ResetText請購人員訊息
            txt請購人員代號.ResetText();
            txt請購人員姓名.ResetText();
            //ResetText存放地點訊息
            txt存放地點代號.ResetText();
            txtName存放地點.ResetText();
            //ResetText廠牌訊息
            txt廠牌代號.ResetText();
            txt廠牌名稱.ResetText();
            //ResetText量具類別訊息
            txt量具類別代號.ResetText();
            txt量具類別名稱.ResetText();
            Load量具訊息();
        }

        public void Load量具訊息()
        {
            ResetTextMeasureTool();
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_量具訊息_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                //dataGV量具台帳.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
                dataGV量具台帳.DataSource = dt;
                PublicClass.setRowNumber(dataGV量具台帳);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load校正類別訊息()
        {
            txt校正類別代號.ResetText();
            txt校正類別名稱.ResetText();
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_校正類別訊息_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGV校正類別訊息.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGV校正類別訊息.DataSource = dt;
                PublicClass.setRowNumber(dataGV校正類別訊息);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load請購人員訊息()
        {
            txt請購人員代號.ResetText();
            txt請購人員姓名.ResetText();
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_請購人員訊息_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGV請購人員.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGV請購人員.DataSource = dt;
                PublicClass.setRowNumber(dataGV請購人員);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load存放地點訊息()
        {
            txt存放地點代號.ResetText();
            txtName存放地點.ResetText();
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_存放地點訊息_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGV存放地點.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGV存放地點.DataSource = dt;
                PublicClass.setRowNumber(dataGV存放地點);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load廠牌訊息()
        {
            txt廠牌代號.ResetText();
            txt廠牌名稱.ResetText();
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_廠牌訊息_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGV廠牌.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGV廠牌.DataSource = dt;
                PublicClass.setRowNumber(dataGV廠牌);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load量具類別訊息()
        {
            txt量具類別代號.ResetText();
            txt量具類別名稱.ResetText();
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_量具類別訊息_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGV量具類別.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGV量具類別.DataSource = dt;
                PublicClass.setRowNumber(dataGV量具類別);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        string tbl1, tbl2, tbl3, tbl4, tbl5, tbl6;
        string lingyong;
        private void btn_領用_Click(object sender, EventArgs e)
        {
            lingyong = "1";
            int r = dataGV量具台帳.CurrentCell.RowIndex;
            string id1 = dataGV量具台帳.Rows[r].Cells[0].Value.ToString();
            string id6 = dataGV量具台帳.Rows[r].Cells[1].Value.ToString();
            string id2 = dataGV量具台帳.Rows[r].Cells[6].Value.ToString();
            string id3 = dataGV量具台帳.Rows[r].Cells[8].Value.ToString();
            string id4 = dataGV量具台帳.Rows[r].Cells[2].Value.ToString();
            string id5 = dataGV量具台帳.Rows[r].Cells[11].Value.ToString();
            tbl1 = id1; //tb1 0
            tbl2 = id2; //cb1 1
            tbl3 = id3; //tb2 2
            tbl4 = id4; //tb3 3
            tbl5 = id5; //cb2 4
            tbl6 = id6;
            frmlingyong frm = new frmlingyong(lingyong, null, tbl1, tbl2, tbl3, tbl4, tbl5, tbl6, null, null, null, null, null, b量具Search, b量具Insert, b量具Edit, b量具Del);
            frm.ShowDialog();
            Load量具訊息();
        }

        string jieyong, tbj1, tbj2, tbj3, tbj4, tbj5, tbj6;
        private void btn_借用_Click(object sender, EventArgs e)
        {          
            jieyong = "1";
            try
            {
                int r = dataGV量具台帳.CurrentCell.RowIndex;
                string id1 = dataGV量具台帳.Rows[r].Cells[0].Value.ToString();
                string id2 = dataGV量具台帳.Rows[r].Cells[1].Value.ToString();
                string id3 = dataGV量具台帳.Rows[r].Cells[2].Value.ToString();
                string id4 = dataGV量具台帳.Rows[r].Cells[6].Value.ToString();
                string id5 = dataGV量具台帳.Rows[r].Cells[8].Value.ToString();
                string id6 = dataGV量具台帳.Rows[r].Cells[11].Value.ToString();
                tbj1 = id1;
                tbj2 = id2;
                tbj3 = id3;
                tbj4 = id4;
                tbj5 = id5;
                tbj6 = id6;

                frmjieyong frm = new frmjieyong(jieyong, null, tbj1, tbj2, tbj3, tbj4, tbj5, tbj6, null, null, null, null, null, null, null, null, b量具Search, b量具Insert, b量具Edit, b量具Del);
                frm.ShowDialog();
                Load量具訊息();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        string tbs1, tbs2, tbs3, tbs4, tbs5, tbs6, tbs7, tbs8, tbs9;
        string Songjian;
        private void btn_送檢_Click(object sender, EventArgs e)
        {
            Songjian = "1";
            int r = dataGV量具台帳.CurrentCell.RowIndex;
            string id1 = dataGV量具台帳.Rows[r].Cells[0].Value.ToString();
            string id6 = dataGV量具台帳.Rows[r].Cells[1].Value.ToString();
            string id2 = dataGV量具台帳.Rows[r].Cells[6].Value.ToString();
            string id3 = dataGV量具台帳.Rows[r].Cells[15].Value.ToString();
            string id4 = dataGV量具台帳.Rows[r].Cells[2].Value.ToString();
            string id5 = dataGV量具台帳.Rows[r].Cells[11].Value.ToString();
            string id7 = dataGV量具台帳.Rows[r].Cells[7].Value.ToString();
            string id8 = dataGV量具台帳.Rows[r].Cells[4].Value.ToString();
            string id9 = dataGV量具台帳.Rows[r].Cells[10].Value.ToString();
            tbs1 = id1; //tb1 0
            tbs2 = id2; //cb1 1
            tbs3 = id3; //tb2 2
            tbs4 = id4; //tb3 3
            tbs5 = id5; //cb2 4
            tbs6 = id6;
            tbs7 = id7;
            tbs8 = id8;
            tbs9 = id9;

            frmSongjian frm = new frmSongjian(Songjian, null, tbs1, tbs2, tbs3, tbs4, tbs5, tbs6, tbs7, tbs8, tbs9, null, null, null, null, null, null, null, null, null, b量具Search, b量具Insert, b量具Edit, b量具Del);
            frm.ShowDialog();
            Load量具訊息();
        }

        string tbx1, tbx2, tbx3, tbx4, tbx5, tbx6;
        string xiuli;
        private void btn_修理_Click(object sender, EventArgs e)
        {
            xiuli = "1";
            int r = dataGV量具台帳.CurrentCell.RowIndex;
            string id1 = dataGV量具台帳.Rows[r].Cells[0].Value.ToString();
            string id6 = dataGV量具台帳.Rows[r].Cells[1].Value.ToString();
            string id2 = dataGV量具台帳.Rows[r].Cells[6].Value.ToString();
            string id3 = dataGV量具台帳.Rows[r].Cells[8].Value.ToString();
            string id4 = dataGV量具台帳.Rows[r].Cells[2].Value.ToString();
            string id5 = dataGV量具台帳.Rows[r].Cells[11].Value.ToString();
            tbx1 = id1; //tb1 0
            tbx2 = id2; //cb1 1
            tbx3 = id3; //tb2 2
            tbx4 = id4; //tb3 3
            tbx5 = id5; //cb2 4
            tbx6 = id6;

            frmXiuLi frm = new frmXiuLi(xiuli, null, tbx1, tbx2, tbx3, tbx4, tbx5, tbx6, null, null, null, null, null, null, null, null, b量具Search, b量具Insert, b量具Edit, b量具Del);
            frm.ShowDialog();
            Load量具訊息();
        }
        string tbf1, tbf2, tbf3, tbf4, tbf5, tbf6;
        string baofei;
        private void btn_報廢_Click(object sender, EventArgs e)
        {
            baofei = "1";
            int r = dataGV量具台帳.CurrentCell.RowIndex;
            string id1 = dataGV量具台帳.Rows[r].Cells[0].Value.ToString();
            string id6 = dataGV量具台帳.Rows[r].Cells[1].Value.ToString();
            string id2 = dataGV量具台帳.Rows[r].Cells[6].Value.ToString();
            string id3 = dataGV量具台帳.Rows[r].Cells[8].Value.ToString();
            string id4 = dataGV量具台帳.Rows[r].Cells[2].Value.ToString();
            string id5 = dataGV量具台帳.Rows[r].Cells[11].Value.ToString();
            tbf1 = id1; //tb1 0
            tbf2 = id2; //cb1 1
            tbf3 = id3; //tb2 2
            tbf4 = id4; //tb3 3
            tbf5 = id5; //cb2 4
            tbf6 = id6;
            frmbaofei frm = new frmbaofei(baofei, null, tbf1, tbf2, tbf3, tbf4, tbf5, tbf6, null, null, null, null, null, null,b量具Search, b量具Insert, b量具Edit, b量具Del);
            frm.ShowDialog();
            Load量具訊息();
        }
        
        private void btn_add_Click(object sender, EventArgs e)
        {
            flag = "1";
            if (!b量具Insert)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                frm量具詳細 frm = new frm量具詳細(flag, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
                frm.ShowDialog();
                //timer1.Enabled = true;
                //this.Close();
                Load量具訊息();
            }
            
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            flag = "2";
            if (!b量具Edit)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    int r = dataGV量具台帳.CurrentCell.RowIndex;
                    string id1 = dataGV量具台帳.Rows[r].Cells[0].Value.ToString();
                    string id2 = dataGV量具台帳.Rows[r].Cells[1].Value.ToString();
                    string id3 = dataGV量具台帳.Rows[r].Cells[2].Value.ToString();
                    string id4 = dataGV量具台帳.Rows[r].Cells[3].Value.ToString();
                    string id5 = dataGV量具台帳.Rows[r].Cells[4].Value.ToString();
                    string id6 = dataGV量具台帳.Rows[r].Cells[5].Value.ToString();
                    string id7 = dataGV量具台帳.Rows[r].Cells[6].Value.ToString();
                    string id8 = dataGV量具台帳.Rows[r].Cells[7].Value.ToString();
                    string id9 = dataGV量具台帳.Rows[r].Cells[8].Value.ToString();
                    string id10 = dataGV量具台帳.Rows[r].Cells[9].Value.ToString();
                    string id20 = dataGV量具台帳.Rows[r].Cells[10].Value.ToString();
                    string id11 = dataGV量具台帳.Rows[r].Cells[11].Value.ToString();
                    //string id21 = dataGV量具台帳.Rows[r].Cells[12].Value.ToString();
                    string id12 = dataGV量具台帳.Rows[r].Cells[12].Value.ToString();
                    string id13 = dataGV量具台帳.Rows[r].Cells[13].Value.ToString();
                    string id14 = dataGV量具台帳.Rows[r].Cells[14].Value.ToString();
                    string id15 = dataGV量具台帳.Rows[r].Cells[15].Value.ToString();
                    //string id22 = dataGV量具台帳.Rows[r].Cells[17].Value.ToString();
                    string id16 = dataGV量具台帳.Rows[r].Cells[16].Value.ToString();
                    string id17 = dataGV量具台帳.Rows[r].Cells[17].Value.ToString();
                    object id23 = dataGV量具台帳.Rows[r].Cells[18].Value;
                    string id18 = dataGV量具台帳.Rows[r].Cells[19].Value.ToString();
                    string id21 = dataGV量具台帳.Rows[r].Cells[20].Value.ToString();

                    tb1 = id1; //tb1 0
                    tb2 = id2; //cb1 1
                    tb3 = id3; //tb2 2
                    tb4 = id4; //tb3 3
                    tb5 = id5; //cb2 4
                    tb6 = id6; //cb3 5
                    tb7 = id7; //tb4 6
                    tb8 = id8; //cb4 7
                    tb9 = id9; //cb5 8
                    tb10 = id10; //tb5 9
                    tb11 = id11; //tb6 11
                    tb12 = id12; //cb6 13
                    tb13 = id13; //tb7 14
                    tb14 = id14; //tb8 15
                    tb15 = id15; //tb9 16
                    tb16 = id16; //tb10 18
                    tb17 = id17; //tb11 19
                    tb18 = id18; //tb12 21
                    //tb19 = id19; //tb13 22....Url img
                    dt1 = id20; //dt1 10
                    //dt2 = id21; //dt2 12
                    //dt3 = id22; //dt3 17
                    img = id23;//img1 20
                    tb20 = id21; //tb20 20

                    frm量具詳細 frm = new frm量具詳細(flag, tb1, tb2, tb3, tb4, tb5, tb6, tb7, tb8, tb9, tb10, tb11, tb12, tb13, tb14, tb15, tb16, tb17, tb18, tb19, dt1, img, tb20);
                    frm.ShowDialog();
                    //this.Close();
                    Load量具訊息();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            if (!b量具Del)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    int r = dataGV量具台帳.CurrentCell.RowIndex;
                    string delete = dataGV量具台帳.Rows[r].Cells[0].Value.ToString();
                    String sql = "SELECT COUNT(*) FROM 量具訊息 WHERE 量具編號='" + delete + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        DialogResult answer = MessageBox.Show("您確定要出入這個量具碼", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            cmd0 = new SqlCommand("sp_量具訊息_刪除", con);
                            
                            cmd0.CommandType = CommandType.StoredProcedure;
                            cmd0.Parameters.AddWithValue("@量具編號", delete);
                            cmd0.ExecuteNonQuery();
                            MessageBox.Show("刪除成功");
                            Load量具訊息();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Error! 資料在使用刪除不成功");
                    }
                    con.Close();
                }
                catch
                {
                    MessageBox.Show("Error! 資料在使用刪除不成功");
                    return;
                }finally { con.Close(); }
            }
            
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //End Code 量具台帳

        //Code校正類別訊息
        private void btn_resetall_Click(object sender, EventArgs e)
        {
            cmb量具狀態.Items.Clear();
            ResetTextMeasureTool();
            AddCommobox();
            Load量具訊息();
            PublicClass.setRowNumber(dataGV量具台帳);
        }

        private void btnAddnews_Click(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                if (txt校正類別代號.Text == "")
                {
                    MessageBox.Show("請您輸入校正類別代號", "錯誤", MessageBoxButtons.OK);
                    txt校正類別代號.Focus();
                    return;
                }

                if (txt校正類別名稱.Text == "")
                {
                    MessageBox.Show("請您輸入校正類別名稱", "錯誤", MessageBoxButtons.OK);
                    txt校正類別名稱.Focus();
                    return;
                }
                //bat dau insert vao database

                con.Open();
                string sp_insert = "sp_校正類別訊息_新增";
                SqlCommand cmd = new SqlCommand(sp_insert, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@校正類別代號", txt校正類別代號.Text);
                cmd.Parameters.AddWithValue("@校正類別名稱", txt校正類別名稱.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("新增成功");
                Load校正類別訊息();
                txt校正類別代號.ResetText();
                txt校正類別名稱.ResetText();
                con.Close();
            }
        }

        private void btnResetText_Click(object sender, EventArgs e)
        {
            txt校正類別代號.ResetText();
            txt校正類別名稱.ResetText();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (!b量具Edit)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt校正類別代號.Text == "") { MessageBox.Show("請您輸入校正類別代號更新"); }
                    else
                    {
                        con.Open();
                        SqlCommand cmd3;
                        String sql = "SELECT COUNT(*) FROM 校正類別訊息 WHERE 校正類別代號=N'" + str + "'";
                        cmd3 = new SqlCommand(sql, con);
                        if ((int)cmd3.ExecuteScalar() > 0)
                        {
                            string sp_update = "sp_校正類別訊息_更新";
                            SqlCommand cmd2 = new SqlCommand(sp_update, con);
                            cmd2.CommandType = CommandType.StoredProcedure;
                            cmd2.Parameters.AddWithValue("@校正類別代號", str);
                            cmd2.Parameters.AddWithValue("@校正類別代號2", txt校正類別代號.Text);
                            cmd2.Parameters.AddWithValue("@校正類別名稱", txt校正類別名稱.Text);
                            cmd2.ExecuteNonQuery();
                            Load校正類別訊息();
                            MessageBox.Show("更新成功");
                            txt校正類別代號.ResetText();
                            txt校正類別名稱.ResetText();
                            con.Close();
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Error! '" + txt校正類別代號.Text.ToString() + "' 不由來");
                            con.Close();
                            return;
                        }
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Load校正類別訊息();
                    Close();
                    return;
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!b量具Del)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt校正類別代號.Text == "") { MessageBox.Show("請您輸入校正類別代號刪除"); }
                    else
                    {

                        DialogResult answer = MessageBox.Show("您確定要出入校正類別代號碼", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            con.Open();
                            string sp_delete = "sp_校正類別訊息_刪除";
                            SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                            sqldelete.CommandType = CommandType.StoredProcedure;
                            sqldelete.Parameters.AddWithValue("@校正類別代號", txt校正類別代號.Text);
                            sqldelete.ExecuteNonQuery();
                            Load校正類別訊息();
                            MessageBox.Show("刪除成功");
                            txt校正類別代號.ResetText();
                            txt校正類別名稱.ResetText();
                            con.Close();
                            return;
                        }
                        else
                        {
                            Load校正類別訊息();
                            txt校正類別代號.ResetText();
                            txt校正類別名稱.ResetText();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    con.Close();
                    MessageBox.Show(ex.ToString());
                    return;
                }
            }
        }

        private void dataGV校正類別訊息_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int r = e.RowIndex;
                str = dataGV校正類別訊息.Rows[r].Cells[0].Value.ToString();
                txt校正類別代號.Text = dataGV校正類別訊息.Rows[r].Cells[0].Value.ToString();
                txt校正類別名稱.Text = dataGV校正類別訊息.Rows[r].Cells[1].Value.ToString();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //End Code 校正類別訊息

        //COde 請購人員
        private void btnResetText2_Click(object sender, EventArgs e)
        {
            Load請購人員訊息();
            txt請購人員代號.ResetText();
            txt請購人員姓名.ResetText();
        }

        private void btnAddnews2_Click(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt請購人員代號.Text == "")
                    {
                        MessageBox.Show("請您輸入請購人員代號", "錯誤", MessageBoxButtons.OK);
                        txt請購人員代號.Focus();
                        return;
                    }

                    if (txt請購人員姓名.Text == "")
                    {
                        MessageBox.Show("請您輸入請購人員姓名", "錯誤", MessageBoxButtons.OK);
                        txt請購人員姓名.Focus();
                        return;
                    }
                    //bat dau insert vao database

                    con.Open();
                    string sp_insert = "sp_請購人員訊息_新增";
                    SqlCommand cmd = new SqlCommand(sp_insert, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@請購人員代號", txt請購人員代號.Text);
                    cmd.Parameters.AddWithValue("@請購人員姓名", txt請購人員姓名.Text);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("新增成功");
                    Load請購人員訊息();
                    con.Close();
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
        }

        private void btnEdit2_Click(object sender, EventArgs e)
        {
            if (!b量具Edit)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt請購人員代號.Text == "") { MessageBox.Show("請您輸入請購人員代號更新"); }
                    else
                    {
                        con.Open();
                        SqlCommand cmd3;
                        String sql = "SELECT COUNT(*) FROM 請購人員訊息 WHERE 請購人員代號=N'" + str + "'";
                        cmd3 = new SqlCommand(sql, con);
                        if ((int)cmd3.ExecuteScalar() > 0)
                        {
                            string sp_update = "sp_請購人員訊息_更新";
                            cmd3 = new SqlCommand(sp_update, con);
                            cmd3.CommandType = CommandType.StoredProcedure;
                            cmd3.Parameters.AddWithValue("@請購人員代號", str);
                            cmd3.Parameters.AddWithValue("@請購人員代號2", txt請購人員代號.Text);
                            cmd3.Parameters.AddWithValue("@請購人員姓名", txt請購人員姓名.Text);

                            cmd3.ExecuteNonQuery();
                            Load請購人員訊息();
                            MessageBox.Show("更新成功");
                            con.Close();
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Error! '" + txt請購人員代號.Text.ToString() + "' 不由來");
                            con.Close();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Load請購人員訊息();
                    Close();
                    return;
                }
            }
        }

        private void btnDelete2_Click(object sender, EventArgs e)
        {
            if (!b量具Del)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt請購人員代號.Text == "") { MessageBox.Show("請您輸入請購人員代號刪除"); }
                    else
                    {

                        DialogResult answer = MessageBox.Show("您確定要出入請購人員代號碼", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            con.Open();
                            string sp_delete = "sp_請購人員訊息_刪除";
                            SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                            sqldelete.CommandType = CommandType.StoredProcedure;
                            sqldelete.Parameters.AddWithValue("@請購人員代號", txt請購人員代號.Text);
                            sqldelete.ExecuteNonQuery();
                            Load請購人員訊息();
                            MessageBox.Show("刪除成功");
                            con.Close();
                            return;
                        }
                        else
                        {
                            Load請購人員訊息();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    con.Close();
                    MessageBox.Show(ex.ToString());
                    return;
                }
            }
        }

        private void dataGV請購人員_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int r = e.RowIndex;
                str = dataGV請購人員.Rows[r].Cells[0].Value.ToString();
                txt請購人員代號.Text = dataGV請購人員.Rows[r].Cells[0].Value.ToString();
                txt請購人員姓名.Text = dataGV請購人員.Rows[r].Cells[1].Value.ToString();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
        //End Code 請購人員
        //Code 存放地點訊息
        private void btnResetText3_Click(object sender, EventArgs e)
        {
            txt存放地點代號.ResetText();
            txtName存放地點.ResetText();
        }

        private void dataGV存放地點_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int r = e.RowIndex;
            str = dataGV存放地點.Rows[r].Cells[0].Value.ToString();
            txt存放地點代號.Text = dataGV存放地點.Rows[r].Cells[0].Value.ToString();
            txtName存放地點.Text = dataGV存放地點.Rows[r].Cells[1].Value.ToString();
        }

        private void btnAddnews3_Click(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt存放地點代號.Text == "")
                    {
                        MessageBox.Show("請您輸入存放地點代號", "錯誤", MessageBoxButtons.OK);
                        txt存放地點代號.Focus();
                        return;
                    }

                    if (txtName存放地點.Text == "")
                    {
                        MessageBox.Show("請您輸入存放地點", "錯誤", MessageBoxButtons.OK);
                        txtName存放地點.Focus();
                        return;
                    }
                    //bat dau insert vao database

                    con.Open();
                    string sp_insert = "sp_存放地點訊息_新增";
                    SqlCommand cmd = new SqlCommand(sp_insert, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@存放地點代號", txt存放地點代號.Text);
                    cmd.Parameters.AddWithValue("@存放地點", txtName存放地點.Text);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("新增成功");
                    Load存放地點訊息();
                    con.Close();
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
        }

        private void btnEdit3_Click(object sender, EventArgs e)
        {
            if (!b量具Edit)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt存放地點代號.Text == "") { MessageBox.Show("請您輸入存放地點代號更新"); }
                    else
                    {
                        con.Open();
                        SqlCommand cmd3;
                        String sql = "SELECT COUNT(*) FROM 存放地點訊息 WHERE 存放地點代號=N'" + str + "'";
                        cmd3 = new SqlCommand(sql, con);
                        if ((int)cmd3.ExecuteScalar() > 0)
                        {
                            string sp_update = "sp_存放地點訊息_更新";
                            cmd3 = new SqlCommand(sp_update, con);
                            cmd3.CommandType = CommandType.StoredProcedure;
                            cmd3.Parameters.AddWithValue("@存放地點代號", str);
                            cmd3.Parameters.AddWithValue("@存放地點代號2", txt存放地點代號.Text);
                            cmd3.Parameters.AddWithValue("@存放地點", txtName存放地點.Text);

                            cmd3.ExecuteNonQuery();
                            Load存放地點訊息();
                            MessageBox.Show("更新成功");
                            con.Close();
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Error! '" + txt存放地點代號.Text.ToString() + "' 不由來");
                            con.Close();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Load存放地點訊息();
                    Close();
                    return;
                }
            }
        }

        private void btnDelete3_Click(object sender, EventArgs e)
        {
            if (!b量具Del)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt存放地點代號.Text == "") { MessageBox.Show("請您輸入存放地點代號刪除"); }
                    else
                    {

                        DialogResult answer = MessageBox.Show("您確定要出入存放地點代號碼", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            con.Open();
                            string sp_delete = "sp_存放地點訊息_刪除";
                            SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                            sqldelete.CommandType = CommandType.StoredProcedure;
                            sqldelete.Parameters.AddWithValue("@存放地點代號", txt存放地點代號.Text);
                            sqldelete.ExecuteNonQuery();
                            Load存放地點訊息();
                            MessageBox.Show("刪除成功");
                            con.Close();
                            return;
                        }
                        else
                        {
                            Load存放地點訊息();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    con.Close();
                    MessageBox.Show(ex.ToString());
                    return;
                }
            }
        }
        //End Code 存放地點訊息

        //Code 廠牌訊息
        private void btnResetText4_Click(object sender, EventArgs e)
        {
            txt廠牌代號.ResetText();
            txt廠牌名稱.ResetText();
        }

        private void dataGV廠牌_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int r = e.RowIndex;
            str = dataGV廠牌.Rows[r].Cells[0].Value.ToString();
            txt廠牌代號.Text = dataGV廠牌.Rows[r].Cells[0].Value.ToString();
            txt廠牌名稱.Text = dataGV廠牌.Rows[r].Cells[1].Value.ToString();
        }

        private void btnAddnews4_Click(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt廠牌代號.Text == "")
                    {
                        MessageBox.Show("請您輸入廠牌代號", "錯誤", MessageBoxButtons.OK);
                        txt廠牌代號.Focus();
                        return;
                    }

                    if (txt廠牌名稱.Text == "")
                    {
                        MessageBox.Show("請您輸入廠牌名稱", "錯誤", MessageBoxButtons.OK);
                        txt廠牌名稱.Focus();
                        return;
                    }
                    //bat dau insert vao database

                    con.Open();
                    string sp_insert = "sp_廠牌訊息_新增";
                    SqlCommand cmd = new SqlCommand(sp_insert, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@廠牌代號", txt廠牌代號.Text);
                    cmd.Parameters.AddWithValue("@廠牌名稱", txt廠牌名稱.Text);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("新增成功");
                    Load廠牌訊息();
                    con.Close();
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
        }

        private void btnEdit4_Click(object sender, EventArgs e)
        {
            if (!b量具Edit)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt廠牌代號.Text == "") { MessageBox.Show("請您輸入廠牌代號更新"); }
                    else
                    {
                        con.Open();
                        SqlCommand cmd3;
                        String sql = "SELECT COUNT(*) FROM 廠牌訊息 WHERE 廠牌代號=N'" + str + "'";
                        cmd3 = new SqlCommand(sql, con);
                        if ((int)cmd3.ExecuteScalar() > 0)
                        {
                            string sp_update = "sp_廠牌訊息_更新";
                            cmd3 = new SqlCommand(sp_update, con);
                            cmd3.CommandType = CommandType.StoredProcedure;

                            cmd3.Parameters.AddWithValue("@廠牌代號", str);
                            cmd3.Parameters.AddWithValue("@廠牌代號2", txt廠牌代號.Text);
                            cmd3.Parameters.AddWithValue("@廠牌名稱", txt廠牌名稱.Text);

                            cmd3.ExecuteNonQuery();
                            Load廠牌訊息();
                            MessageBox.Show("更新成功");
                            con.Close();
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Error! '" + txt廠牌代號.Text.ToString() + "' 不由來");
                            con.Close();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Load廠牌訊息();
                    Close();
                    return;
                }
            }
        }

        private void btnDelete4_Click(object sender, EventArgs e)
        {
            if (!b量具Del)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt廠牌代號.Text == "") { MessageBox.Show("請您輸入廠牌代號刪除"); }
                    else
                    {

                        DialogResult answer = MessageBox.Show("您確定要出入廠牌代號碼", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            con.Open();
                            string sp_delete = "sp_廠牌訊息_刪除";
                            SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                            sqldelete.CommandType = CommandType.StoredProcedure;
                            sqldelete.Parameters.AddWithValue("@廠牌代號", txt廠牌代號.Text);
                            sqldelete.ExecuteNonQuery();
                            Load廠牌訊息();
                            MessageBox.Show("刪除成功");
                            con.Close();
                            return;
                        }
                        else
                        {
                            Load廠牌訊息();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    con.Close();
                    MessageBox.Show(ex.ToString());
                    return;
                }
            }
        }
        //End Code 廠牌訊息

        //Code 量具類別訊息
        private void btnResetText5_Click(object sender, EventArgs e)
        {
            txt量具類別代號.ResetText();
            txt量具類別名稱.ResetText();
        }

        private void dataGV量具類別_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int r = e.RowIndex;
            DataGridViewRow rows = new DataGridViewRow();
            rows = dataGV量具類別.Rows[r];
            str = rows.Cells[0].Value.ToString();
            txt量具類別代號.Text = rows.Cells[0].Value.ToString();
            txt量具類別名稱.Text = rows.Cells[1].Value.ToString();
        }

        private void btnAddnews5_Click(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt量具類別代號.Text == "")
                    {
                        MessageBox.Show("請您輸入量具類別代號", "錯誤", MessageBoxButtons.OK);
                        txt量具類別代號.Focus();
                        return;
                    }

                    if (txt量具類別名稱.Text == "")
                    {
                        MessageBox.Show("請您輸入量具類別名稱", "錯誤", MessageBoxButtons.OK);
                        txt量具類別名稱.Focus();
                        return;
                    }
                    //bat dau insert vao database

                    con.Open();
                    string sp_insert = "sp_量具類別訊息_新增";
                    SqlCommand cmd = new SqlCommand(sp_insert, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@量具類別代號", txt量具類別代號.Text);
                    cmd.Parameters.AddWithValue("@量具類別名稱", txt量具類別名稱.Text);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("新增成功");
                    Load量具類別訊息();
                    con.Close();
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
        }

        private void btnEdit5_Click(object sender, EventArgs e)
        {
            if (!b量具Edit)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt量具類別代號.Text.ToString() == null || txt量具類別代號.Text.ToString().Equals("")) { MessageBox.Show("請您輸入量具類別代號更新"); }
                    else
                    {
                        con.Open();
                        SqlCommand cmd3;
                        String sql = "SELECT COUNT(*) FROM 量具類別訊息 WHERE 量具類別代號=N'" + str + "'";
                        cmd3 = new SqlCommand(sql, con);
                        if ((int)cmd3.ExecuteScalar() > 0)
                        {
                            string sp_update = "sp_量具類別訊息_更新";
                            cmd3 = new SqlCommand(sp_update, con);
                            cmd3.CommandType = CommandType.StoredProcedure;
                            cmd3.Parameters.AddWithValue("@量具類別代號", str);
                            cmd3.Parameters.AddWithValue("@量具類別代號2", txt量具類別代號.Text);
                            cmd3.Parameters.AddWithValue("@量具類別名稱", txt量具類別名稱.Text);
                            cmd3.ExecuteNonQuery();
                            Load量具類別訊息();
                            MessageBox.Show("更新成功");
                            con.Close();
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Error! '" + txt量具類別代號.Text.ToString() + "' 不由來");
                            con.Close();
                            return;
                        }
                        
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Load量具類別訊息();
                    Close();
                    return;
                }
            }
        }

        private void btnDelete5_Click(object sender, EventArgs e)
        {
            if (!b量具Del)
            {
                MessageBox.Show("很抱歉，您沒有權限！", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (txt量具類別代號.Text == "") { MessageBox.Show("請您輸入量具類別代號刪除"); }
                    else
                    {

                        DialogResult answer = MessageBox.Show("您確定要出入量具類別代號碼", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            con.Open();
                            string sp_delete = "sp_量具類別訊息_刪除";
                            SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                            sqldelete.CommandType = CommandType.StoredProcedure;
                            sqldelete.Parameters.AddWithValue("@量具類別代號", txt量具類別代號.Text);
                            sqldelete.ExecuteNonQuery();
                            Load量具類別訊息();
                            MessageBox.Show("刪除成功");
                            con.Close();
                            return;
                        }
                        else
                        {
                            Load量具類別訊息();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    con.Close();
                    MessageBox.Show(ex.ToString());
                    return;
                }
            }
        }

        private void dataGV量具類別_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV量具類別);
        }

        private void dataGV廠牌_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV廠牌);
        }

        private void dataGV存放地點_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV存放地點);
        }

        private void dataGV請購人員_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV請購人員);
        }

        private void dataGV校正類別訊息_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV校正類別訊息);
        }

        private void dataGV量具台帳_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV量具台帳);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Load量具訊息();
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            if (!b量具Search)
            {
                MessageBox.Show("很抱歉，您没有权限","錯誤",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            SqlConnection con = new SqlConnection(connectionString);

            try
            {
                con.Open();
                string sp;
                sp = "sp_量具訊息_尋找";
                //if (cmb量具狀態.Text.Equals("所有"))
                //    sp = "sp_量具訊息_尋找";
                if (cmb量具狀態.Text.Equals("領用"))
                    sp = "sp_領用紀錄_load";
                if (cmb量具狀態.Text.Equals("借用"))
                    sp = "sp_借用_load";
                if (cmb量具狀態.Text.Equals("送檢"))
                    sp = "sp_送檢_load";
                if (cmb量具狀態.Text.Equals("修理"))
                    sp = "sp_修理_load";
                if (cmb量具狀態.Text.Equals("報廢"))
                    sp = "sp_報廢_load";
                //if (cmb量具狀態.Text.Equals("維修"))
                //    sp = "sp_維修_load";
                if (cmb量具狀態.Text.Equals("校正"))
                    sp = "sp_校正_load";
                SqlCommand cmd = new SqlCommand(sp, con);
                cmd.CommandType = CommandType.StoredProcedure;
                if (cmb量具狀態.Text.ToString() != "") cmd.Parameters.AddWithValue("@量具編號", txt量具類別.Text.ToString());
                if (txt量具名稱.Text != "") cmd.Parameters.AddWithValue("@量具名稱", txt量具名稱.Text);
                if (txt校正類別.Text != "") cmd.Parameters.AddWithValue("@校正類別代號", txt校正類別.Text);
                if (txt量具類別.Text != "") cmd.Parameters.AddWithValue("@量具類別代號", txt量具類別.Text);
                if (txt存放地點.Text != "") cmd.Parameters.AddWithValue("@存放地點代號", txt存放地點.Text);
                if (txt数量.Text != "") cmd.Parameters.AddWithValue("@数量", txt数量.Text);
                //cmd.Parameters.AddWithValue("@出廠日期", txt数量.Text);
                cmd.Parameters.AddWithValue("@datefrom", datefrom.Value);
                cmd.Parameters.AddWithValue("@todate", todate.Value);
                //SqlDataReader sqlreader = cmd.ExecuteReader();
                //DataTable dt = new DataTable();
                //==========
                if (cmb量具狀態.Text.Equals("所有"))
                {                  
                    SqlDataReader sqlreader = cmd.ExecuteReader();
                    DataTable dt = new DataTable();
                    dt.Columns.Add("量具編號");
                    dt.Columns.Add("量具狀態");
                    dt.Columns.Add("量具名稱");
                    dt.Columns.Add("材料編號");
                    dt.Columns.Add("校正類別代號");
                    dt.Columns.Add("量具類別代號");
                    dt.Columns.Add("規格型號");
                    dt.Columns.Add("廠牌代號");
                    dt.Columns.Add("存放地點代號");
                    dt.Columns.Add("管理類別");
                    dt.Columns.Add("出廠日期");
                    dt.Columns.Add("測量範圍");
                    //dt.Columns.Add("購買日期");
                    dt.Columns.Add("請購人員代號");
                    dt.Columns.Add("外檢機構");
                    dt.Columns.Add("出廠編號");
                    dt.Columns.Add("測量精度");
                    //dt.Columns.Add("入庫日期");
                    dt.Columns.Add("內檢週期");
                    dt.Columns.Add("外檢週期");
                    //dt.Columns.Add("照片");
                    dt.Columns.Add("備註");
                    dt.Columns.Add("数量");
                    while (sqlreader.Read())
                    {
                        DataRow dtrows = dt.NewRow();
                        dtrows["量具編號"] = sqlreader["量具編號"];
                        dtrows["量具狀態"] = sqlreader["量具狀態"];
                        dtrows["量具名稱"] = sqlreader["量具名稱"];
                        dtrows["材料編號"] = sqlreader["材料編號"];
                        dtrows["校正類別代號"] = sqlreader["校正類別代號"];
                        dtrows["量具類別代號"] = sqlreader["量具類別代號"];
                        dtrows["規格型號"] = sqlreader["規格型號"];
                        dtrows["廠牌代號"] = sqlreader["廠牌代號"];
                        dtrows["存放地點代號"] = sqlreader["存放地點代號"];
                        dtrows["管理類別"] = sqlreader["管理類別"];
                        dtrows["出廠日期"] = sqlreader["出廠日期"];
                        dtrows["測量範圍"] = sqlreader["測量範圍"];
                        //dtrows["購買日期"] = sqlreader["購買日期"];
                        dtrows["請購人員代號"] = sqlreader["請購人員代號"];
                        dtrows["外檢機構"] = sqlreader["外檢機構"];
                        dtrows["出廠編號"] = sqlreader["出廠編號"];
                        dtrows["測量精度"] = sqlreader["測量精度"];
                        //dtrows["入庫日期"] = sqlreader["入庫日期"];
                        dtrows["內檢週期"] = sqlreader["內檢週期"];
                        dtrows["外檢週期"] = sqlreader["外檢週期"];
                        //dtrows["照片"] = sqlreader["照片"];
                        dtrows["備註"] = sqlreader["備註"];
                        dtrows["数量"] = sqlreader["数量"];
                        dt.Rows.Add(dtrows);
                    }
                    dataGV量具台帳.DataSource = dt;
                    PublicClass.setRowNumber(dataGV量具台帳);
                }

                if (cmb量具狀態.Text == "領用")
                {
                    //SqlConnection con = new SqlConnection(connectionString);
                    //con.Open();
                    ////string sp_load = "sp_領用紀錄_load";
                    SqlDataAdapter da = new SqlDataAdapter(sp, con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    dataGV量具台帳.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                    dataGV量具台帳.DataSource = dt;
                    PublicClass.setRowNumber(dataGV量具台帳);
                    dataGV量具台帳.Columns[0].Width = 0;
                    con.Close();
                }
                if (cmb量具狀態.Text == "送檢")
                {
                    //SqlConnection con = new SqlConnection(connectionString);
                    //con.Open();
                    //string sp_load = "sp_送檢_load";
                    SqlDataAdapter da = new SqlDataAdapter(sp, con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    dataGV量具台帳.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                    dataGV量具台帳.DataSource = dt;
                    PublicClass.setRowNumber(dataGV量具台帳);
                    dataGV量具台帳.Columns[0].Width = 0;
                    con.Close();
                }
                if (cmb量具狀態.Text == "借用")
                {
                    //SqlConnection con = new SqlConnection(connectionString);
                    //con.Open();
                    //string sp_load = "sp_借用_load";
                    SqlDataAdapter da = new SqlDataAdapter(sp, con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    dataGV量具台帳.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                    dataGV量具台帳.DataSource = dt;
                    PublicClass.setRowNumber(dataGV量具台帳);
                    dataGV量具台帳.Columns[0].Width = 0;
                    con.Close();
                }
                if (cmb量具狀態.Text == "修理")
                {
                    //SqlConnection con = new SqlConnection(connectionString);
                    //con.Open();
                    //string sp_load = "sp_修理_load";
                    SqlDataAdapter da = new SqlDataAdapter(sp, con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    dataGV量具台帳.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                    dataGV量具台帳.DataSource = dt;
                    PublicClass.setRowNumber(dataGV量具台帳);
                    dataGV量具台帳.Columns[0].Width = 0;
                    con.Close();
                }
                if (cmb量具狀態.Text == "報廢")
                {
                    //SqlConnection con = new SqlConnection(connectionString);
                    //con.Open();
                    //string sp_load = "sp_報廢_load";
                    SqlDataAdapter da = new SqlDataAdapter(sp, con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    dataGV量具台帳.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                    dataGV量具台帳.DataSource = dt;
                    PublicClass.setRowNumber(dataGV量具台帳);
                    dataGV量具台帳.Columns[0].Width = 0;
                    con.Close();
                }
                //if (cmb量具狀態.Text == "維修")
                //{
                //    //SqlConnection con = new SqlConnection(connectionString);
                //    //con.Open();
                //    //string sp_load = "sp_維修_load";
                //    SqlDataAdapter da = new SqlDataAdapter(sp, con);
                //    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                //    DataTable dt = new DataTable();
                //    da.Fill(dt);
                //    dataGV量具台帳.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                //    dataGV量具台帳.DataSource = dt;
                //    PublicClass.setRowNumber(dataGV量具台帳);
                //    dataGV量具台帳.Columns[0].Width = 0;
                //    con.Close();
                //}
                if (cmb量具狀態.Text == "校正")
                {
                    //SqlConnection con = new SqlConnection(connectionString);
                    //con.Open();
                    //string sp_load = "sp_校正_load";
                    SqlDataAdapter da = new SqlDataAdapter(sp, con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    dataGV量具台帳.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                    dataGV量具台帳.DataSource = dt;
                    PublicClass.setRowNumber(dataGV量具台帳);
                    dataGV量具台帳.Columns[0].Width = 0;
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void btPrinter_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("Watting...", "通報", MessageBoxButtons.OK);
            Export_Excel excel = new Export_Excel();
            DataTable dt = (DataTable)dataGV量具台帳.DataSource;
            excel.Export(dt, "Sheet1", "量具台帳" + cmb量具狀態.SelectedItem.ToString());
        }
        string jiaozheng, tbz1, tbz2, tbz3, tbz4, tbz5, tbz6;
        private void btn_校正_Click(object sender, EventArgs e)
        {
            jiaozheng = "1";
            try
            {
                int r = dataGV量具台帳.CurrentCell.RowIndex;
                string id1 = dataGV量具台帳.Rows[r].Cells[0].Value.ToString();
                string id2 = dataGV量具台帳.Rows[r].Cells[1].Value.ToString();
                string id3 = dataGV量具台帳.Rows[r].Cells[2].Value.ToString();
                string id4 = dataGV量具台帳.Rows[r].Cells[6].Value.ToString();
                string id5 = dataGV量具台帳.Rows[r].Cells[8].Value.ToString();
                string id6 = dataGV量具台帳.Rows[r].Cells[11].Value.ToString();
                tbz1 = id1; 
                tbz2 = id2; 
                tbz3 = id3; 
                tbz4 = id4; 
                tbz5 = id5; 
                tbz6 = id6;

                frmJiaoZheng frm = new frmJiaoZheng(jiaozheng, null, tbz1, tbz2, tbz3, tbz4, tbz5, tbz6, null, null, null, null, null, null, null, null, b量具Search, b量具Insert, b量具Edit, b量具Del);
                frm.ShowDialog();
                Load量具訊息();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txt数量_TextChanged(object sender, EventArgs e)
        {

        }

        private void txt数量_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) || e.KeyChar == '.')
            {
                e.Handled = true;
                MessageBox.Show("請您輸入號碼", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            //else if (e.KeyChar == '.')
            //{
            //    MessageBox.Show("不能輸入圈点", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
        }
        //End Code 量具類別訊息
    }
}
